package org.marsson.bNav;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.Random;

/**
 * @author Marsson
 *Class that is responsible for auto-filling the ships on the grid.
 *it generates a robot that will move the mouse, and physically set all the required
 *ships.
 */
public class AutoShipFiller implements Runnable {
	Robot robot;
	private static final int steps = 200;
	Point button1;
	Point button2;
	
	PlayingField playingField;
	MouseInfo mouseinfo;
	Point gridPosition;
	boolean vertical = false;

	public AutoShipFiller(PlayingField playingField)  {
		
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.playingField = playingField;

		
		gridPosition = new Point(playingField.getMyOcean().getLocationOnScreen());
		button1 = new Point( playingField.seawolfB.getLocationOnScreen());
		button2 = new Point(playingField.battleshipB.getLocationOnScreen());
		button1 = invPoint.invert(button1);
		button1 = invPoint.addSome(button1);
		button2 = invPoint.addSome(button2);
		button2= invPoint.invert(button2);
		gridPosition = invPoint.invert(gridPosition);
	}

	public void placeShip(){
		gotoPoint(getRandomPoint());
		if(playingField.getMyOcean().validPlacement()==false){
			placeShip();
		}
	}
	
	public Point getRandomPoint(){
	Random generator = new Random();
	int x = gridPosition.x+generator.nextInt( playingField.gridSize )*25+10;	
	int y = gridPosition.y+generator.nextInt(playingField.gridSize)*25+10;
	vertical = generator.nextBoolean();
	return new Point(x,y);
	}
	
	public void gotoPoint(Point point) {
		Point now = mouseinfo.getPointerInfo().getLocation();
		System.out.println("Now vale" + now);

		try {
			robot = new Robot();
		} catch (AWTException e) {

			e.printStackTrace();
		}
		double vertical = now.getY();
		double horizontal = now.getX();
		double verticalcoef = (point.getX() - vertical) / steps;
		double horizontalcoef = (point.getY() - horizontal) / steps;

		// System.out.printf("Mouse is now on %f,%f , there will be %d steps final position will be %f,%f\n",vertical,hori,steps,200*xcoef+x,200*ycoef+y);

		for (int i = 0; i < steps; i++) {
			robot.delay(10); // introduce delay to see the movements

			// increment the co-ordinates
			vertical = vertical + verticalcoef;
			horizontal = horizontal + horizontalcoef;
			robot.mouseMove((int) horizontal, (int) vertical);
			System.out
					.println("Mouse is now on " + vertical + "," + horizontal);

		}
		try {
			Thread.sleep(160);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(this.vertical==true){
			robot.mousePress(InputEvent.BUTTON3_MASK);
			robot.mouseRelease(InputEvent.BUTTON3_MASK);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		this.vertical=false;
		
		}
		
		
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("ESTOU SENDO EXECUTADO!!!");
		gotoPoint(button1);
		placeShip();
		gotoPoint(button2);
		placeShip();
		gotoPoint(button1);
		placeShip();
		gotoPoint(button2);
		placeShip();
		gotoPoint(button1);
		
	}
}
